//
//  NRWCarLongPressView.m
//  New_Runway
//
//  Created by 张记 on 2018/5/28.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWCarLongPressView.h"
#import "NRWCarUrlShortModel.h"
@interface NRWCarLongPressView ()

@end

@implementation NRWCarLongPressView

-(void)dismiss{
    [self removeFromSuperview];
}
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:CGRectMake(frame.size.width/2, 0, 0, frame.size.height)];
    if (self) {
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = frame.size.height/2;
        UIColor *color = [UIColor blackColor];
        self.backgroundColor = [color colorWithAlphaComponent:0.5];
        [self animation:frame];
        UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(event:)];
        [self addGestureRecognizer:tapGesture];
        [tapGesture setNumberOfTapsRequired:1];
        [self addButtonToCopyLink];
        
    }
    return self;
}
-(void)animation:(CGRect)frame{
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.frame = frame;
        self.layer.cornerRadius = 0;
    } completion:^(BOOL finished) {
        
    }];
}
-(void)addButtonToCopyLink{
    UIButton *copyB = [[UIButton alloc] init];
    copyB.layer.masksToBounds = YES;
    copyB.layer.cornerRadius = 5;
    copyB.backgroundColor = kColorWhite();
    [copyB setTitle:@"复制链接" forState:UIControlStateNormal];
    [copyB setTitleColor:UIColorHex(0x3186DC) forState:UIControlStateNormal];
    [copyB.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [copyB addTarget:self action:@selector(copyThisCarLink:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:copyB];
    kSelfWeak;
    [copyB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(weakSelf);
        make.height.mas_equalTo(35);
        make.width.mas_equalTo(90);
    }];
    
}
- (void)event:(UITapGestureRecognizer *)gesture{
    [self dismiss];
}
-(void)setModel:(Result *)model{
    _model = model;
}
-(void)copyThisCarLink:(UIButton *)sender{
   
   
   NSString *long_url = [NSString stringWithFormat:@"%@/mshop/src/cars_share.html?marketId=%@&shopId=%@&imgUrl=%@&customerShopId=false&carId=%@&os=false&shareUserName=%@&shareUserMobile=%@", Api, _model.marketId, _model.shopId, ImageApi, _model.carId,[NRWCarUser sharedUser].userName,[NRWCarUser sharedUser].userMobile];
   NSString *encodeUrl = [long_url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
   [self loadShortLink:encodeUrl];
        
}
-(void)loadShortLink:(NSString *)url{
    
    [YQNetworking postWithUrl:@"http://api.t.sina.com.cn/short_url/shorten.json" refreshRequest:YES cache:NO params:@{@"source":@"3271760578",@"url_long":url} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        NSArray *array = response;
        if (array.count > 0) {
            NSDictionary *dic = array[0];
            NRWCarUrlShortModel *model = [NRWCarUrlShortModel mj_objectWithKeyValues:dic];
            NSString *url_short = model.url_short;
            UIPasteboard * pastboard = [UIPasteboard generalPasteboard];
            pastboard.string = url_short;
            [ShowMessage showHintMessage:@"复制链接成功" duration:1 withRemindId:remindSuccess];
            [self dismiss];
        }
    
    } failBlock:^(NSError *error) {
        
    }];
}

@end
